﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;

namespace PyramidShadow
{
    class Map
    {
        // The image representing the parallaxing background
        private Texture2D background;
        private Texture2D colorMap;
        private Vector2 position;
        private Level level;
        private List<LevelObject> levelObjects;
        public List<LevelObject> Levelobjects
        {
            get { return levelObjects; }
        }

        public Texture2D ColorMap
        {
            get { return colorMap; }
        }

        public Map(Texture2D bg,Texture2D cm, Level level, MapBuilder mapContent)
        {
            this.level = level;
            background = bg;
            colorMap = cm;
            // Set the position of the background
            position = new Vector2(
                (level.Game.Window.ClientBounds.Width / 2) - (bg.Width / 2),
                (level.Game.Window.ClientBounds.Height / 2) - (bg.Height / 2));

            levelObjects = new List<LevelObject>();

            /**************************************************************************
             * The order is important so the kollison and reset methods work properly!*
             **************************************************************************/

            initBlocks(mapContent.BlockMembers);
            initSigns(mapContent.SignMembers);
            initExit(mapContent.ExitMembers);
            initFloorButtons(mapContent.FloorButtonMembers);
            
        }

        public void Update(GameTime gameTime, KeyboardState currentKeyboardState)
        {
            foreach (LevelObject lo in levelObjects)
                lo.Update(gameTime, currentKeyboardState);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (LevelObject lo in levelObjects)
                lo.Draw(spriteBatch);

            spriteBatch.Draw(background, position, Color.White);
        }

        private void initExit(exit_Members content)
        {
            if (content.exit_pos == null || content.exit_tex == null)
                return;

            levelObjects.Add(new Exit(content.exit_tex,content.exit_pos,level);
        }

        private void initBlocks(block_Members content)
        {
            if (content.block_positions == null || content.block_tex == null)
                return;
            Texture2D block_texture = content.block_tex;
            List<Vector2> block_positions = content.block_positions;
            foreach (Vector2 pos in block_positions)
            {
                levelObjects.Add(new Block(block_texture,pos,level);
            }
        }

        private void initFloorButtons(floorButton_Members content)
        {
            if (content.floorButton_events == null ||
                content.floorButton_frameDim == null ||
                content.floorButton_offset == null ||
                content.floorButton_positions == null ||
                content.floorButton_reset == null ||
                content.floorButton_tex == null)
                return;
            
            for (int i = 0; i < content.floorButton_positions.Length; i++)
            {
                if (content.floorButton_events[i] == null ||
                    content.floorButton_positions[i] == null ||
                    content.floorButton_reset[i] == null)
                    continue;
                levelObjects.Add(new FloorButton(content.floorButton_tex,
                                                 content.floorButton_positions[i],
                                                 level,
                                                 content.floorButton_frameDim,
                                                 content.floorButton_offset,
                                                 content.floorButton_reset[i],
                                                 content.floorButton_events[i]));
            }
        }

        private void initSigns(sign_Members content)
        {
            if (content.sign_events == null ||
                content.sign_positions == null ||
                content.sign_tex == null ||
                content.sign_textFields == null)
                return;

            for (int i = 0; i < content.sign_positions.Length; i++)
            {
                if (content.sign_textFields[i] == null || content.sign_positions[i] == null)
                    continue;
                if (content.sign_events[i] == null)
                    levelObjects.Add(new Sign(content.sign_tex,
                                              content.sign_positions[i],
                                              level,
                                              content.sign_textFields[i]));
                else
                    levelObjects.Add(new Sign(content.sign_tex,
                                              content.sign_positions[i],
                                              level,
                                              content.sign_events[i],
                                              content.sign_textFields[i]));
            }
            
        }
    }
}
